<?php
	class settingModel extends MY_Model
	{
		function __construct()
		{
			// Call the Model constructor
			parent::__construct();
			$this->table = "site_setting";
			$this->tableLan = "site_settingLan";
			$this->table_id = "code";		
		}
		
		function get_setting($arr_lang = array()){
			$query = $this->db->get($this->table, 1);
			if($query->num_rows < 1){
				$query->free_result();
				$this->db->delete($this->tableLan, array("code  != " => 0));
				$this->db->insert($this->table, array("code" => 1));
				foreach($arr_lang as $key => $lan){
					$this->db->insert($this->tableLan, array("code" => 1, "lan" => $key));
				}
				$query = $this->db->get($this->table, 1);
			}
			return $query->row_array();
		}
		
		function get_settingLan($arr = array()){
			$query = $this->db->get_where($this->tableLan, $arr);			
			return $query->row_array();
		}
		
		function get_site_general($type, $arr_lang = array()){
			$query = $this->db->get_where("site_general", array("type" => $type));
			if($query->num_rows < 1){
				$query->free_result();
				$this->db->insert("site_general", array("type" => $type));
				$code = $this->db->insert_id();
				foreach($arr_lang as $key => $lan){
					$this->db->insert("site_generalLan", array("code" => $code, "lan" => $key));
				}
				$query = $this->db->get_where("site_general", array("type" => $type));
			}
			return $query->row_array();
		}
		
		function get_site_generalLan($arr){
			$query = $this->db->get_where("site_generalLan", $arr);
			return $query->row_array();
		}
		
		function update_site_general($code, $arr_lang, $data){
			//update site_generalLan
			$today = date("Y/m/d H:i:s");
			$this->db->update("site_general", array("upDate" => $today), array("code" => $code));
			foreach($arr_lang as $key => $lan){
				//$content = mysql_real_escape_string($data[lang_key($key, "content")]);
				$content = $data[lang_key($key, "content")];				
				$this->db->update("site_generalLan", array("content" => $content), array("code" => $code, "lan" => $key));
			}
			$query = $this->db->get_where("site_general", array("code" => $code));
			return $query->row()->type;
		}
	}
?>